package com.nextel.project.pedido.bean;

import pe.com.nextel.rms.DataInputStreamX;
import pe.com.nextel.rms.DataOutputStreamX;
import pe.com.nextel.rms.PersistenteX;
import java.io.IOException;

import org.kxml2.io.KXmlParser;
import org.kxml2.io.KXmlSerializer;
import pe.com.nextel.util.ArrayList;
import pe.com.nextel.util.Iterator;
import pe.com.nextel.util.List;
import pe.com.nextel.util.StringUtils;

/**
 *
 * @author eespinoza
 * aMendiola 14/09/2010
 */
public class ArticuloBean extends PersistenteX {

    private String codigo;
    private String nombre;
    private String stock;
    private String precioBase;
    private List listaPrecio;

    public ArticuloBean() {
        setListaPrecio(new ArrayList());
    }

    public void persistir(DataOutputStreamX dataOutput, List list) throws IOException {
        //     System.out.println("ArticuloBean.persistir");
        dataOutput.writeInt(list.size());

        for (Iterator it = list.iterator(); it.hasNext();) {
            ArticuloBean bean = (ArticuloBean) it.next();
            bean.persistirItem(dataOutput);
        }
    }

    public void persistirItem(DataOutputStreamX dataOutput) throws IOException {
        //   System.out.println("ArticuloBean.persistirItem");

        dataOutput.writeString(StringUtils.stringNullToBlank(getCodigo()));
        dataOutput.writeString(StringUtils.stringNullToBlank(getNombre()));
        dataOutput.writeString(StringUtils.stringNullToBlank(getStock()));
        dataOutput.writeString(StringUtils.stringNullToBlank(getPrecioBase()));
        
        int i = listaPrecio.size();
        dataOutput.writeInt(i);

        if (i > 0) {
            LPrecioBean precio = new LPrecioBean();
            precio.persistir(dataOutput, listaPrecio);
            precio = null;
        }
        
    }

    public void recuperarItem(DataInputStreamX dataInput) throws IOException {

        this.setCodigo(dataInput.readString());
        this.setNombre(dataInput.readString());
        this.setStock(dataInput.readString());
        this.setPrecioBase(dataInput.readString());

        //System.out.println("codigo id | pos | stock " + this.getIdRecodset() + " | " + this.getPosicionID() + " | "  + this.getCodigo() + " | " + this.getStock());
        
        int i = dataInput.readInt();

        if (i > 0) {
            LPrecioBean precio = new LPrecioBean();
            setListaPrecio(precio.recuperar(dataInput));
            precio = null;
        }        
    }
    
    public List recuperar(DataInputStreamX dataInput) throws IOException {
        //System.out.print("ArticuloBean.recuperar ");
        List lista = new ArrayList();
        ArticuloBean bean = null;

        int cant = dataInput.readInt();

        for (int i = 0; i < cant; i++) {
            bean = new ArticuloBean();
            bean.recuperarItem(dataInput);
            lista.add(bean);
        }

        return lista;
    }

    public String getPrecioCanal(String codigoCanal) {
        LPrecioBean precio=null;
        for (Iterator it = getListaPrecio().iterator(); it.hasNext();) {
            precio = (LPrecioBean) it.next();
            //System.out.println("|"+codigoCanal + "|" + precio.getCodigoCanal()+"|");
            if (precio.getCodigoCanal().equals(codigoCanal)) {
                return precio.getPrecio();
            }
        }
        return precioBase;
    }

    public void writeXMLObject(KXmlSerializer writer) throws IOException {

        writer.startTag(null, "a");
        writer.attribute(null, "c", StringUtils.stringNullToBlank(getCodigo()));
        writer.attribute(null, "n", StringUtils.stringNullToBlank(getNombre()));

        writer.endTag(null, "a");

    }

    public void readXMLObject(KXmlParser parser) throws IOException {
//
//        setCodigo(parser.getAttributeValue(null, "c"));
//        setNombre(parser.getAttributeValue(null, "n"));
//


    }


    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getStock() {
        return stock;
    }

    public void setStock(String stock) {
        this.stock = stock;
    }

//    public void writeXMLObject(KXmlSerializer writer) throws IOException {
//    }
//
//    public void readXMLObject(KXmlParser parser) throws IOException {
//        //setCodigo(parser.getAttributeValue(null, "c"));
//        //setNombre(parser.getAttributeValue(null, "n"));
//        //setStock(parser.getAttributeValue(null, "s"));
//    }

    
    public List getListaPrecio() {
        return listaPrecio;
    }

    public void setListaPrecio(List listaPrecio) {
        this.listaPrecio = listaPrecio;
    }
        
    public void clear() {
        if (listaPrecio != null) {
            listaPrecio.clear();
            listaPrecio = null;
        }
    }
    
    public String getPrecioBase() {
        return precioBase;
    }

    public void setPrecioBase(String precioBase) {
        this.precioBase = precioBase;
    }
}
